<template>
  <div class="users">
    <!-- 面包屑导航区 -->
    <el-card class="breadcrumb-box">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item
          ><a href="/#/manager/home">首页</a></el-breadcrumb-item
        >
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>

    <el-card>
      <!-- 用户列表 -->
      <el-table :data="userList" border style="width: 100%" v-loading="loading">
        <el-table-column
          type="index"
          width="60"
          align="center"
          label="序号"
        ></el-table-column>
        <el-table-column
          prop="userName"
          align="center"
          label="姓名"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="userEmail"
          align="center"
          label="邮箱"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="userTel" label="电话" align="center" width="120">
          <template #default="scope">
            {{ scope.row.userTel }}
          </template>
        </el-table-column>
        <el-table-column
          prop="userRoleId"
          label="角色"
          align="center"
          width="120"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.userRoleId === 3">普通用户</el-tag>
            <el-tag v-else-if="scope.row.userRoleId === 2" type="success"
              >管理员</el-tag
            >
            <el-tag type="warning" v-else>顶级管理员</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="userStudyAllTime"
          label="用户总学习时间"
          align="center"
          width="140"
        ></el-table-column>
        <el-table-column
          prop="userCreateTime"
          label="用户创建时间"
          align="center"
          width="160"
        ></el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="160">
          <template #default="scope">
            <el-tooltip content="分配权限" placement="top" :enterable="false">
              <!-- 分配权限按钮 -->
              <el-button
                type="primary"
                :icon="Setting"
                @click="showUpdateUserRoleIdDialog(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top" :enterable="false">
              <!-- 删除按钮 -->
              <el-button
                type="danger"
                :icon="Delete"
                @click="deleteUser(scope.row.userId)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
      title="修改权限"
      v-model="updateUserRoleIdDialogVisible"
      @close="newRoleId = 0"
      width="30%"
      style="text-align: left;"
    >
      <el-select
        @change="getRoleId"
        v-model="newRoleId"
        placeholder="请选择"
        style="width: 100%"
      >
        <el-option
          v-for="item in roleIdOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="footer">
        <el-button @click="updateUserRoleIdDialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="updateUserRoleId">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus';
import { Setting, Delete } from '@element-plus/icons-vue'
import { ref, getCurrentInstance, ComponentInternalInstance, onMounted, reactive } from 'vue';

const { appContext } = getCurrentInstance() as ComponentInternalInstance;
const proxy = appContext.config.globalProperties;

// 用户列表
interface UserList {
  userId: Number
  userName: String
  userTel: String
}
const userList = ref<UserList[]>([]);
// 获取用户列表的页码参数对象
const queryInfo = reactive({
  // 当前的页数
  currentPage: 1,
  // 当前每页显示的数据条数
  pageSize: 10,
});
// 用户列表总条数
const total = ref(0);
// 是否在加载中
const loading = ref(false);
// 权限分配对话框是否可见
const updateUserRoleIdDialogVisible = ref(false);
const roleIdOptions = ref([
  {
    value: 3,
    label: '普通用户',
  },
  {
    value: 2,
    label: '管理员',
  },
  {
    value: 1,
    label: '顶级管理员',
  },
]);
// 用户权限id
const userId = ref(0);
const newRoleId = ref(0);

onMounted(() => {
  getUserList();
})

// 获取用户列表
function getUserList() {
  loading.value = true;
  proxy.$request.manager.getAllUser(queryInfo).then((res: any) => {
    userList.value = res.data.list;
    total.value = res.data.total;
    userList.value.forEach((item, index) => {
      userList.value[index].userTel = safetyTel(item.userTel);
    });
    loading.value = false;
  });
};
// 安全电话显示
function safetyTel(tel: any) {
  if (!tel) return;
  let reg = /^(\d{3})\d{4}(\d{4})$/;
  return tel.replace(reg, '$1****$2');
};
// 监听 pagesize 改变
function handleSizeChange(newSize: any) {
  queryInfo.pageSize = newSize;
  getUserList();
};
// 监听页码值改变
function handleCurrentChange(newPage: any) {
  queryInfo.currentPage = newPage;
  getUserList();
};
// 展示分配权限对话框
function showUpdateUserRoleIdDialog(row: any) {
  userId.value = row.userId;
  newRoleId.value = row.userRoleId;
  updateUserRoleIdDialogVisible.value = true;
};
// 更新用户权限
function updateUserRoleId() {
  proxy.$request.manager
    .updateUserRoleId({
      managerId: proxy.$storage.getItem('userInfo').userRoleId,
      userId: userId.value,
      userNewRoleId: newRoleId.value,
    })
    .then((res: any) => {
      ElMessage({
        message: res.msg,
        type: 'success',
        duration: 1000,
      });
      getUserList();
      updateUserRoleIdDialogVisible.value = false;
    });
};
// 当用户选择发生改变时触发
function getRoleId(value: any) {
  newRoleId.value = value;
};
// 删除用户
function deleteUser(userId: any) {
  ElMessageBox.confirm('此操作将删除该用户, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      proxy.$request.manager.deleteUser({ userId: userId }).then((res: any) => {
        ElMessage({
          message: res.msg,
          type: 'success',
          duration: 1000,
        });
        getUserList();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消操作',
        duration: 1000,
      });
    });
};
</script>

<style lang="less" scoped>
.breadcrumb-box {
  position: fixed;
  height: 36px;
  border-radius: 0;
  top: 60px;
  width: 100%;
  padding: 0 20px;
  border: 0;
  margin: 0 0 20px !important;
  z-index: 9;

  .el-breadcrumb {
    line-height: 36px;
  }

  /deep/ .el-card__body {
    padding: 0;
  }
}

.el-card:nth-child(2) {
  margin: 60px 20px 0;
}

.el-pagination {
  margin-top: 20px;
  justify-content: center;
}

.footer {
  display: flex;
  justify-content: end;
  width: 100%;
  margin-top: 20px;
}
</style>
